// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.process.parts;

import java.beans.PropertyChangeEvent;

import org.dengues.designer.ui.database.figures.DBNoteFigure;
import org.dengues.designer.ui.editors.ProcessMultiEditor;
import org.dengues.designer.ui.editors.editmanager.DiagramChildCellEditorLocator;
import org.dengues.designer.ui.editors.editmanager.DiagramChildDirectEditManager;
import org.dengues.designer.ui.process.commands.CompNoteOpacityChangeCommand;
import org.dengues.designer.ui.process.models.CompNote;
import org.dengues.designer.ui.process.policies.CompNoteDirectEditPolicy;
import org.dengues.designer.ui.process.policies.CompNoteEditPolicy;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.gef.tools.DirectEditManager;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;

/**
 * qzhang class global comment. Detailled comment <br/>
 */
public class CompNotePart extends AbstractProcessPart {

    private DirectEditManager directEditManager;

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.process.parts.AbstractProcessPart#getCastModel()
     */
    @Override
    protected CompNote getCastModel() {
        return ((CompNote) getModel());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
     */
    @Override
    protected IFigure createFigure() {
        return new DBNoteFigure();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#refreshVisuals()
     */
    @Override
    protected void refreshVisuals() {
        CompNote note = getCastModel();
        Point location = note.getLocation();
        DBNoteFigure noteFigure = ((DBNoteFigure) getFigure());
        Dimension size = note.getSize();
        noteFigure.setLocation(location);
        noteFigure.setSize(size);
        noteFigure.setText(note.getText());
        noteFigure.setOpaque(note.isOpaque());

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
     */
    @Override
    protected void createEditPolicies() {
        installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new CompNoteDirectEditPolicy());
        installEditPolicy(EditPolicy.COMPONENT_ROLE, new CompNoteEditPolicy());
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
     */
    public void propertyChange(PropertyChangeEvent arg0) {
        refresh();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#performRequest(org.eclipse.gef.Request)
     */
    @Override
    public void performRequest(Request request) {
        if (request.getType() == RequestConstants.REQ_OPEN) {
            IWorkbenchPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
            if (part instanceof ProcessMultiEditor) {
                CommandStack commandStack = (CommandStack) part.getAdapter(CommandStack.class);
                Command command = new CompNoteOpacityChangeCommand(getCastModel(), !getCastModel().isOpaque());
                commandStack.execute(command);
            }
        }
        if (request.getType() == RequestConstants.REQ_DIRECT_EDIT) {
            if (directEditManager == null) {
                DBNoteFigure noteFigure = (DBNoteFigure) getFigure();
                directEditManager = new DiagramChildDirectEditManager(this, TextCellEditor.class,
                        new DiagramChildCellEditorLocator(noteFigure));
            }
            directEditManager.show();
        }
        super.performRequest(request);
    }

}
